package com.netease.financial.common.util.preferences;

import android.text.TextUtils;

import com.netease.financial.common.util.DateUtil;
import com.netease.financial.common.util.PackageUtils;

/**
 * Created by wyf on 7/30/15.
 */
public class AppUpdatePreferencesHelper {

    private static final String PREFERENCES_FILE_NAME = PreferencesHelper.getFullPreferenceFileName("app_update");

    private static final String DATE_FORMAT_APP_UPDATE_DAY = "yyyyMMdd";

    private static final String KEY_LAST_APP_UPDATE_PROMPT_DAY = "last_app_update_prompt_day";
    private static final String KEY_IS_APP_NEED_FORCE_UPDATE = "is_app_need_force_update";

    private static final String KEY_UPDATE_INFO_LATEST_APK_URL = "update_info_latest_apk_url";
    private static final String KEY_UPDATE_INFO_LATEST_VERSION_CODE = "update_info_latest_version_code";

    private static final String KEY_NEED_SHOW_SPLASH_GUIDE = "last_version_code_show_splash_guide";

    private static PreferencesHelper sPreferencesHelper;

    private static PreferencesHelper getPreferencesHelper() {
        if (sPreferencesHelper == null) {
            sPreferencesHelper = PreferencesHelper.newInstance(PREFERENCES_FILE_NAME);
        }
        return sPreferencesHelper;
    }

    public static boolean isSplashGuideNeedShow() {
        int currentVersionCode = PackageUtils.getCurrentApkVersionCode(PreferencesHelper.getContext());
        int lastVersionCodeHasShowSplashGuide = getPreferencesHelper().getInt(KEY_NEED_SHOW_SPLASH_GUIDE, -1);
        return lastVersionCodeHasShowSplashGuide < currentVersionCode;
    }

    public static void setLastVersionCodeForShowSplash() {
        int currentVersionCode = PackageUtils.getCurrentApkVersionCode(PreferencesHelper.getContext());
        getPreferencesHelper().putInt(KEY_NEED_SHOW_SPLASH_GUIDE, currentVersionCode);
    }

    public static void setLastAppUpdatePromptDay(long timeMills) {
        String day = DateUtil.getFormattedDate(timeMills, DATE_FORMAT_APP_UPDATE_DAY);
        getPreferencesHelper().putString(KEY_LAST_APP_UPDATE_PROMPT_DAY, day);
    }

    public static String getLastAppUpdatePromptDay() {
        return getPreferencesHelper().getString(KEY_LAST_APP_UPDATE_PROMPT_DAY, "");
    }

    public static boolean allowPromptAppUpdate() {
        String lastAppUpdatePromptDay = AppUpdatePreferencesHelper.getLastAppUpdatePromptDay();
        if (TextUtils.isEmpty(lastAppUpdatePromptDay)) {
            return true;
        }
        String today = DateUtil.getFormattedDate(System.currentTimeMillis(), DATE_FORMAT_APP_UPDATE_DAY);
        return !lastAppUpdatePromptDay.equals(today);
    }

    public static void setAppNeedForceUpdate(boolean isAppNeedForceUpdate) {
        getPreferencesHelper().putBoolean(KEY_IS_APP_NEED_FORCE_UPDATE, isAppNeedForceUpdate);
    }

    public static boolean isAppNeedForceUpdate() {
        return getPreferencesHelper().getBoolean(KEY_IS_APP_NEED_FORCE_UPDATE, false);
    }

    public static void saveLatestApkUrl(String apkUrl) {
        getPreferencesHelper().putString(KEY_UPDATE_INFO_LATEST_APK_URL, apkUrl);
    }

    public static String getLatestApkUrl() {
        return getPreferencesHelper().getString(KEY_UPDATE_INFO_LATEST_APK_URL, "");
    }

    public static void saveLatestAppVersionCode(int versionCode) {
        getPreferencesHelper().putInt(KEY_UPDATE_INFO_LATEST_VERSION_CODE, versionCode);
    }

    public static int getLatestAppVersionCode() {
        return getPreferencesHelper().getInt(KEY_UPDATE_INFO_LATEST_VERSION_CODE, 0);
    }
}
